package controllers

import (
	"github.com/goravel/framework/contracts/http"
	"github.com/goravel/framework/facades"
	"github.com/jinzhu/copier"
	"goravel/app/models"
	"goravel/app/services"
	"goravel/app/types"
)

type DictDataController struct {
	// Dependent services
}

func NewDictDataController() *DictDataController {
	return &DictDataController{
		// Inject services
	}
}

// Index 数据字典数据列表
// @Summary      数据字典数据列表
// @Tags         Dict
// @Router       /dict_data/{dict_slug} [get]
func (c *DictDataController) Index(ctx http.Context) http.Response {
	dictSlug := ctx.Request().Route("dict_slug")
	var err error
	var modelList []models.SysDictData
	err = facades.Orm().Query().Where("dict_slug", dictSlug).Get(&modelList)
	if err != nil {
		return services.NewApiService(ctx).Error500(err.Error())
	}
	var modelListResp []types.SysDictDataListResp
	err = copier.Copy(&modelListResp, &modelList)
	if err != nil {
		return services.NewApiService(ctx).Error500(err.Error())
	}
	return services.NewApiService(ctx).Data(modelListResp)
}

// Show 数据字典数据详情
// @Summary      数据字典数据详情
// @Tags         Dict
// @Router       /dict_data/{dict_slug}/{id} [get]
func (c *DictDataController) Show(ctx http.Context) http.Response {
	dictSlug := ctx.Request().Route("dict_slug")
	id := ctx.Request().RouteInt64("id")
	var err error
	var model models.SysDictData
	err = facades.Orm().Query().Where("dict_slug", dictSlug).Where("id", id).FirstOrFail(&model)
	if err != nil {
		return services.NewApiService(ctx).Error500(err.Error())
	}
	return services.NewApiService(ctx).Data(model)
}

// Store 新增数据字典数据
// @Summary      新增数据字典数据
// @Tags         Dict
// @Router       /dict_data/{dict_slug} [post]
func (c *DictDataController) Store(ctx http.Context) http.Response {
	dictSlug := ctx.Request().Route("dict_slug")
	var createReq types.SysDictDataCreateReq
	var err error
	err = ctx.Request().Bind(&createReq)
	if err != nil {
		return services.NewApiService(ctx).Error500(err.Error())
	}
	//TODO：检查dictSlug对应的dict是否存在
	var model models.SysDictData
	err = copier.Copy(&model, &createReq)
	if err != nil {
		return services.NewApiService(ctx).Error500("参数copier错误")
	}
	model.DictSlug = dictSlug
	err = facades.Orm().Query().Create(&model)
	return services.NewApiService(ctx).OK("Store")
}

// Update 更新数据字典数据
// @Summary      更新数据字典数据
// @Tags         Dict
// @Router       /dict_data/{dict_slug}/{id} [put]
func (c *DictDataController) Update(ctx http.Context) http.Response {
	dictSlug := ctx.Request().Route("dict_slug")
	id := ctx.Request().RouteInt64("id")
	var updateReq types.SysDictDataUpdateReq
	var err error
	err = ctx.Request().Bind(&updateReq)
	if err != nil {
		return services.NewApiService(ctx).Error500(err.Error())
	}
	var model models.SysDictData
	err = facades.Orm().Query().Where("dict_slug", dictSlug).Where("id", id).Get(&model)
	if err != nil {
		return services.NewApiService(ctx).Error500("无相关数据")
	}
	err = copier.CopyWithOption(&model, &updateReq, copier.Option{IgnoreEmpty: true})
	if err != nil {
		return services.NewApiService(ctx).Error500("参数copier错误")
	}
	err = facades.Orm().Query().Save(&model)
	if err != nil {
		return services.NewApiService(ctx).Error500("更新错误")
	}
	return services.NewApiService(ctx).OK("updated")
}

// Destroy 删除数据字典数据
// @Summary      删除数据字典数据
// @Tags         Dict
// @Router       /dict_data/{dict_slug}/{id} [delete]
func (c *DictDataController) Destroy(ctx http.Context) http.Response {
	dictSlug := ctx.Request().Route("dict_slug")
	id := ctx.Request().RouteInt64("id")
	var model models.SysDictData
	var err error
	err = facades.Orm().Query().Where("dict_slug", dictSlug).Where("id", id).Get(&model)
	if err != nil {
		return services.NewApiService(ctx).Error500("无相关数据")
	}
	_, err = facades.Orm().Query().Delete(&model)
	if err != nil {
		return services.NewApiService(ctx).Error500("删除失败")
	}
	return services.NewApiService(ctx).OK("Delete")
}
